/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tableprocessor;
import java.util.Iterator;
import java.util.List;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 *
 * @author gleb
 */
public class DBColumnDAO
{
        private final static Logger logger = LoggerFactory.getLogger(DBColumnDAO.class);
        public static Iterator<DBColumn> getColumnsByCathegory(int cid)
        {
                Transaction tx = null;
            List <DBColumn> rlist = null;
            Session session = SessionFactoryUtil.getInstance().getCurrentSession();
            try
            {
                session.setFlushMode(FlushMode.NEVER);
                tx = session.beginTransaction();
                Query q = session.createQuery("SELECT rlist FROM DBColumn AS rlist WHERE cathegory_id =:cid");//"SELECT rlist FROM DBProduce AS rlist ");
                q.setParameter("cid", cid);
                rlist = q.list();
                tx.commit();
                return rlist.iterator();
            }
            catch (RuntimeException e)
            {
                e.printStackTrace();
                if (tx != null && tx.isActive())
                {
                    try {tx.rollback();}
                    catch (HibernateException e1){e1.printStackTrace();}
                }
            }
            return null;
        }
        public static Iterator<DBColumn> getColumns()
        {
            Transaction tx = null;
            List <DBColumn> rlist = null;
            Session session = SessionFactoryUtil.getInstance().getCurrentSession();
            try
            {
                session.setFlushMode(FlushMode.NEVER);
                tx = session.beginTransaction();
                Query q = session.createQuery("SELECT rlist FROM DBColumn AS rlist");//"SELECT rlist FROM DBProduce AS rlist ");
                rlist = q.list();
                tx.commit();
                return rlist.iterator();
            }
            catch (RuntimeException e)
            {
                e.printStackTrace();
                if (tx != null && tx.isActive())
                {
                    try {tx.rollback();}
                    catch (HibernateException e1){e1.printStackTrace();}
                }
            }
            return null;
        }
        public static void removeColumn(int id)
        {
            Transaction tx = null;
            Session session = SessionFactoryUtil.getInstance().getCurrentSession();
            try
            {

               // session.setFlushMode(FlushMode.NEVER);
                tx = session.beginTransaction();
                Query q = session.createQuery("DELETE FROM DBColumn where id=:idt");
                q.setParameter("idt", id);
                q.executeUpdate();
                q = session.createQuery("DELETE FROM DBProduce where column_id=:idt");
                q.setParameter("idt", id);
                q.executeUpdate();
                //q.
               // session.save(new DBCathegory(name,parent));
                tx.commit();
            }
            catch (RuntimeException e)
            {
                e.printStackTrace();
                if (tx != null && tx.isActive())
                {
                    try {tx.rollback();}
                    catch (HibernateException e1){e1.printStackTrace();}
                }
            }
        }
        public static void removeColumnByCathegoryId(int cid)
        {
            Transaction tx = null;
            Session session = SessionFactoryUtil.getInstance().getCurrentSession();
            try
            {

               // session.setFlushMode(FlushMode.NEVER);
                tx = session.beginTransaction();
                Query q = session.createQuery("DELETE FROM DBColumn where cathegory_id=:idt");
                q.setParameter("idt", cid);
                q.executeUpdate();
               // q = session.createQuery("DELETE FROM DBProduce where column_id=:idt");
                //q.setParameter("idt", id);
                q.executeUpdate();
                //q.
               // session.save(new DBCathegory(name,parent));
                tx.commit();
            }
            catch (RuntimeException e)
            {
                e.printStackTrace();
                if (tx != null && tx.isActive())
                {
                    try {tx.rollback();}
                    catch (HibernateException e1){e1.printStackTrace();}
                }
            }
        }
        public static void addColumn(int _cathegory_id, String _name, int _number)
        {
            System.out.println("add column");
            Transaction tx = null;
            Session session = SessionFactoryUtil.getInstance().getCurrentSession();
            try
            {
               // session.setFlushMode(FlushMode.NEVER);
                tx = session.beginTransaction();
                session.save(new DBColumn(_cathegory_id,_number,_name));
                tx.commit();
            }
            catch (RuntimeException e)
            {
                if (tx != null && tx.isActive())
                {
                    try {tx.rollback();}
                    catch (HibernateException e1){e1.printStackTrace();}
                }
            }
        }
         public static void renameColumn(String name,int cathegory_id, int id)
        {
            Transaction tx = null;
            Session session = SessionFactoryUtil.getInstance().getCurrentSession();
            try
            {

               // session.setFlushMode(FlushMode.NEVER);
                tx = session.beginTransaction();
                DBColumn col = new DBColumn();
                col.setId(id);
                col.setName(name);
                col.setCathegory_id(cathegory_id);
                session.update(col);
                tx.commit();
            }
            catch (RuntimeException e)
            {
                e.printStackTrace();
                if (tx != null && tx.isActive())
                {
                    try {tx.rollback();}
                    catch (HibernateException e1){e1.printStackTrace();}
                }
            }
        }
}
